package icedot.library.common.dialog;

import icedot.library.common.R;
import icedot.library.common.base.BaseActivity;
import icedot.library.common.base.BaseDlgFragment;
import icedot.library.common.utils.StringUtils;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MessageDlgFragment extends BaseDlgFragment
{
	private LinearLayout _layoutOk;
	private LinearLayout _layoutCancel;
	private LinearLayout _layoutSet;
	private TextView _btnOk;
	private TextView _btnCancel;
	private TextView _btnSet;
	private TextView _textContent;
	private TextView _textTitle;
	
	private String _content;
	private String _title;
	private String _okText;
	private String _setText;
	private String _cancelText;
	private int _cancelShow = View.VISIBLE;
	private int _okShow = View.VISIBLE;
	private int _setShow = View.GONE;
	private OnClickListener _cancelListener;
	private OnClickListener _okListener;
	private OnClickListener _setListener;
	
	private OnClickListener _defaultListener = new OnClickListener() 
	{
		@Override
		public void onClick(View v) 
		{
			MessageDlgFragment.this.dismiss();
		}
	};
    
    public MessageDlgFragment()
    {
    	_content = "";
    	_title = "";
    	_okText = "";
    	_cancelText = "";
    	_setText = "";
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
    	setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) 
    {
    	//return super.onCreateView(inflater, container, savedInstanceState);
    	View v = inflater.inflate(R.layout.dlg_message, container, false);
    	_textContent = (TextView)v.findViewById(R.id.dlgmessage_text_content);
    	_textTitle = (TextView)v.findViewById(R.id.dlgmessage_text_title);
    	_btnOk = (TextView)v.findViewById(R.id.dlgmessage_btn_ok);
    	_btnCancel = (TextView)v.findViewById(R.id.dlgmessage_btn_cancel);
    	_btnSet = (TextView)v.findViewById(R.id.dlgmessage_btn_set);
    	_layoutCancel = (LinearLayout)v.findViewById(R.id.dlgmessage_layout_cancel);
    	_layoutOk = (LinearLayout)v.findViewById(R.id.dlgmessage_layout_ok);
    	_layoutSet = (LinearLayout)v.findViewById(R.id.dlgmessage_layout_set);
    	
    	_textContent.setText(_content);
    	_textTitle.setText(_title);
    	if(!StringUtils.isNullOrEmpty(_okText))
    	{
    		_btnOk.setText(_okText);
    	}
    	if(!StringUtils.isNullOrEmpty(_cancelText))
    	{
    		_btnCancel.setText(_cancelText);
    	}
    	if(!StringUtils.isNullOrEmpty(_setText))
    	{
    		_btnSet.setText(_setText);
    	}
  
    	if(_okListener == null)
    	{
    		_btnOk.setOnClickListener(_defaultListener);
    	}
    	else
    	{
    		_btnOk.setOnClickListener(_okListener);
    	}
    	
    	if(_cancelListener == null)
    	{
    		_btnCancel.setOnClickListener(_defaultListener);
    	}
    	else
    	{
    		_btnCancel.setOnClickListener(_cancelListener);
    	}
    	
    	if(_setListener == null)
    	{
    		_btnSet.setOnClickListener(_defaultListener);
    	}
    	else
    	{
    		_btnSet.setOnClickListener(_setListener);
    	}
    	
    	_layoutCancel.setVisibility(_cancelShow);
    	_layoutOk.setVisibility(_okShow);
    	_layoutSet.setVisibility(_setShow);
    	if(_okShow == View.VISIBLE)
    	{
    	    _btnOk.requestFocus();
    	}
    	else if(_setShow == View.VISIBLE)
    	{
    	    _btnSet.requestFocus();
    	}
    	else
    	{
    	    _btnCancel.requestFocus();
    	}
    	return v;
    }
    
    @Override
    public void show(FragmentManager manager, String tag)
    {
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }
    
    public void setOkLinstener(View.OnClickListener linstener)
    {
    	_okListener = linstener;
    	if(_btnOk != null)
    	{
    		_btnOk.setOnClickListener(_okListener);
    	}
    }
    
    public void setCancelLinstener(View.OnClickListener linstener)
    {
    	_cancelListener = linstener;
    	if(_btnCancel != null)
    	{
    		_btnCancel.setOnClickListener(_okListener);
    	}
    }
    public void setSetLinstener(View.OnClickListener linstener)
    {
    	_setListener = linstener;
    	if(_btnSet != null)
    	{
    		_btnSet.setOnClickListener(_setListener);
    	}
    }
    public void setContent(String content)
    {
    	_content = content;
    	if(_textContent != null)
    	{
    		_textContent.setText(_content);
    	}
    }
    public String getContent()
    {
    	return _content;
    }
    public void setTitle(String title)
    {
    	_title = title;
    	if(_textTitle != null)
    	{
    		_textTitle.setText(_title);
    	}
    }
    public String getTitle()
    {
    	return _title;
    }
    public TextView getOKButton()
    {
    	return _btnOk;
    }
    public TextView getCancelButton()
    {
    	return _btnCancel;
    }
    public TextView getSetButton()
    {
    	return _btnSet;
    }
    public void setOkButtonText(String text)
    {
    	_okText = text;
    	if(_btnOk != null)
    	{
    		_btnOk.setText(_okText);
    	}
    }
    public void setCancelButtonText(String text)
    {
    	_cancelText = text;
    	if(_btnCancel != null)
    	{
    		_btnCancel.setText(_okText);
    	}
    }
    public void setSetButtonText(String text)
    {
    	_setText = text;
    	if(_btnSet != null)
    	{
    		_btnSet.setText(_okText);
    	}
    }
    public void setOkBtnVisibility(int show)
    {
    	_okShow = show;
    	if(_layoutOk != null)
    	{
    		_layoutOk.setVisibility(show);
    	}
    }
    public void setCancelBtnVisiable(int show)
    {
    	_cancelShow = show;
    	if(_layoutCancel != null)
    	{
    		_layoutCancel.setVisibility(show);
    	}
    }
    public void setSetBtnVisiable(int show)
    {
    	_setShow = show;
    	if(_layoutSet != null)
    	{
    		_layoutSet.setVisibility(show);
    	}
    }
    public void show(BaseActivity context)
    {
    	if(context == null)
    		return;
    	show(context.getSupportFragmentManager(),"messageDlg");
    }
    
	public static MessageDlgFragment showOneBtnDlg(BaseActivity act, String title,String content)
	{
		if(act == null)
			return null;
		final MessageDlgFragment dlg = new MessageDlgFragment();
		dlg.setTitle(title);
		dlg.setContent(content);
		dlg.setCancelBtnVisiable(View.GONE);
		dlg.setSetBtnVisiable(View.GONE);
		dlg.setCancelable(false);
		dlg.setCancelLinstener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dlg.dismiss();
			}
		});
		dlg.show(act);
		return dlg;
	}
}
